﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TicTacToe
{
    public partial class TicTacToeForm : Form
    {
        private char[,] _gameBoard;
        private char _playerTurn;
        public TicTacToeForm()
        {
            InitializeComponent();
            _gameBoard = new char[,] { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
            _playerTurn = 'X';
        }

        public bool CheckWinner(char[,] input)
        {
            // Check rows
            for (int i = 0; i < 3; i++)
            {
                if (input[i, 0] != ' ' && input[i, 0] == input[i, 1] && input[i, 1] == input[i, 2])
                {
                    return true;
                }
            }

            // Check columns
            for (int i = 0; i < 3; i++)
            {
                if (input[0, i] != ' ' && input[0, i] == input[1, i] && input[1, i] == input[2, i])
                {
                    return true;
                }
            }

            // Check diagonals
            if (input[0, 0] != ' ' && input[0, 0] == input[1, 1] && input[1, 1] == input[2, 2])
            {
                return true;
            }

            if (input[2, 0] != ' ' && input[2, 0] == input[1, 1] && input[1, 1] == input[0, 2])
            {
                return true;
            }
            return false;
        }
        public void EndGame()
        {
            label1.Text = _playerTurn + " is the winner!";
            UpperLeftButton.Enabled = false;
            UpperMiddleButton.Enabled = false;
            UpperRightButton.Enabled = false;
            MiddleLeftButton.Enabled = false;
            CenterButton.Enabled = false;
            MiddleRightButton.Enabled = false;
            ButtomLeftButton.Enabled = false;
            BottomMiddleButton.Enabled = false;
            BottomRightButton.Enabled = false;
        }
        private void HandleNextTurn()
        {
            if (CheckWinner(_gameBoard))
            {
                EndGame();
                return;
            }
            var stalemate = true;
            for (int i = 0; i <= 2; i++)
            {
                for (int j = 0; j <= 2; j++)
                {
                    if (_gameBoard[i, j] == ' ')
                    {
                        stalemate = false;
                    }
                }
            }
            if (stalemate)
            {
                label1.Text = "Stalemate";
                return;
            }
            if (_playerTurn == 'X')
            {
                _playerTurn = 'O';
            }
            else
            {
                _playerTurn = 'X';
            }
        }
        private void UpperLeftButton_Click(object sender, EventArgs e)
        {
            _gameBoard[0, 0] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();
            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }
        private void UpperMiddleButton_Click(object sender, EventArgs e)
        {
            _gameBoard[0, 1] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }
        private void UpperRightButton_Click(object sender, EventArgs e)
        {
            _gameBoard[0, 2] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }


        private void MiddleLeftButton_Click(object sender, EventArgs e)
        {
            _gameBoard[1, 0] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }
        private void CenterButton_Click(object sender, EventArgs e)
        {
            _gameBoard[1, 1] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }
        private void MiddleRightButton_Click(object sender, EventArgs e)
        {
            _gameBoard[1, 2] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }
        private void BottomLeftButton_Click(object sender, EventArgs e)
        {
            _gameBoard[2, 0] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }
        private void BottomMiddleButton_Click(object sender, EventArgs e)
        {
            _gameBoard[2, 1] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = true;

            HandleNextTurn();
        }
        private void BottomRightButton_Click(object sender, EventArgs e)
        {
            _gameBoard[2, 2] = _playerTurn;
            ((Button)sender).Text = _playerTurn.ToString();

            ((Button)sender).Enabled = false;
            HandleNextTurn();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            _gameBoard = new char[,] { { ' ', ' ', ' ' }, { ' ', ' ', ' ' }, { ' ', ' ', ' ' } };
            _playerTurn = 'X';
            label1.Text = "";
            UpperLeftButton.Enabled = true;
            UpperLeftButton.Text = " ";
            UpperMiddleButton.Enabled = true;
            UpperMiddleButton.Text = " ";
            UpperRightButton.Enabled = true;
            UpperRightButton.Text = " ";
            MiddleLeftButton.Enabled = true;
            MiddleLeftButton.Text = " ";
            CenterButton.Enabled = true;
            CenterButton.Text = " ";
            MiddleRightButton.Enabled = true;
            MiddleRightButton.Text = " ";
            ButtomLeftButton.Enabled = true;
            ButtomLeftButton.Text = " ";
            BottomMiddleButton.Enabled = true;
            BottomMiddleButton.Text = " ";
            BottomRightButton.Enabled = true;
            BottomRightButton.Text = " ";
        }
    }
}
